<?php
/**
 * 支付
 *
 *
 *
 *
 */

class member_paymentControl extends mobileMemberControl {

    private $payment_code = '';

	public function __construct() {
		parent::__construct();
	}

    // 通联支付
    function tonglianpayOp(){
        $minfo = get_mobile_member_info();
        require $_SERVER['DOCUMENT_ROOT'] . '/shop/api/payment/tl2/tl2.php';
        $tl2 = new tl2;
        $result = $tl2->mobile_get_code();
        if($result['retcode'] == 'SUCCESS'){
            echo $result['payinfo'];
        }
        else{
            echo 'failed';
        }

    }

    // 汇聚支付1 [H5]
    function joinpay1Op(){
        require $_SERVER['DOCUMENT_ROOT'] . '/mobile/api/payment/joinpay1/joinpay1.php';
        $joinpay1 = new joinPay1();
        $result = $joinpay1->mobile_get_code();
        $arr_result = json_decode($result, true);
        if($arr_result["ra_Code"] == "100"){  
            output_data($result);
        }
        else{
            output_error($arr_result["rb_CodeMsg"]);
        }
    }

    // 汇聚支付2 [公众号]
    function joinpay2Op(){
        require $_SERVER['DOCUMENT_ROOT'] . '/mobile/api/payment/joinpay2/joinpay2.php';
        $joinpay2 = new joinPay2();
        $result = $joinpay2->mobile_get_code();
        if($result["ra_Code"] == "100"){
            output_data($result);
        }
        else{
            output_error('failed');
        }
    }

    /**
     * 实物订单支付
     */
    public function payOp() {
	    $pay_sn        = $_GET['pay_sn'];
        $payment_type  = $_GET['payment_type'];

        $this->payment_code = $payment_type;
        $model_mb_payment = Model('mb_payment');
        $logic_payment = Logic('payment');

        $condition = array();
        $condition['payment_code'] = $this->payment_code;

        $mb_payment_info = $model_mb_payment->getMbPaymentOpenInfo($condition);

        if(!$mb_payment_info) {
            output_error('系统不支持选定的支付方式');
        }

        //重新计算所需支付金额
        $result = $logic_payment->getRealOrderInfo($pay_sn, $this->member_info['member_id']);

        if(!$result['state']) {
            output_error($result['msg']);
        }

        //第三方API支付
        $this->_api_pay($result['data'], $mb_payment_info);
    }

    /**
     * 虚拟订单支付
     */
    public function vr_payOp() {
        $order_sn = $_GET['pay_sn'];

        $model_mb_payment = Model('mb_payment');
        $logic_payment = Logic('payment');

        $condition = array();
        $condition['payment_code'] = $this->payment_code;
        $mb_payment_info = $model_mb_payment->getMbPaymentOpenInfo($condition);
        if(!$mb_payment_info) {
            output_error('系统不支持选定的支付方式');
        }

        //重新计算所需支付金额
        $result = $logic_payment->getVrOrderInfo($order_sn, $this->member_info['member_id']);

        if(!$result['state']) {
            output_error($result['msg']);
        }

        //第三方API支付
        $this->_api_pay($result['data'], $mb_payment_info);
    }

	/**
	 * 第三方在线支付接口
	 *
	 */
	private function _api_pay($order_pay_info, $mb_payment_info) {
        $inc_file = BASE_PATH.DS.'api'.DS.'payment'.DS.$this->payment_code.DS.$this->payment_code.'.php';
    	if(!is_file($inc_file)){
            output_error('支付接口不存在');
    	}
    	require($inc_file);
        $param = array();
    	$param = $mb_payment_info['payment_config'];
        $param['order_sn'] = $order_pay_info['pay_sn'];
        $param['order_amount'] = $order_pay_info['api_pay_amount'];
        $param['order_type'] = ($order_pay_info['order_type'] == 'real_order' ? 'r' : 'v');
        $payment_api = new $this->payment_code();
        $return = $payment_api->submit($param);
        header('Location: '.$return);
    	exit;
	}

    /**
     * 可用支付参数列表
     */
    public function payment_listOp() {
        $model_mb_payment = Model('mb_payment');

        $payment_list = $model_mb_payment->getMbPaymentOpenList();

        $payment_array = array();
        if(!empty($payment_list)) {
            foreach ($payment_list as $value) {
                $payment_array[] = $value['payment_code'];
            }
        }

        output_data(array('payment_list' => $payment_array));
    }
}
